package cn.edu.njust.epms.service.Impl;

import cn.edu.njust.epms.common.R;
import cn.edu.njust.epms.dao.UserDao;
import cn.edu.njust.epms.entity.ModApply;
import cn.edu.njust.epms.entity.User;
import cn.edu.njust.epms.entity.UserDetails;
import cn.edu.njust.epms.service.UserService;
import cn.edu.njust.epms.common.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao dao;

    public Resp<User> Login(User user){
        String name = user.getuName();
        User user1 = dao.selectByName(name);
        if(user1 == null){//查无此人，登陆失败
            return Resp.error("您输入的用户名不存在！");
        }
        String uPw = user.getuPw()+user1.getSalt();
        String s = DigestUtils.md5DigestAsHex(uPw.getBytes());
        System.out.println(s);
        if(!s.equals(user1.getuPw())){//密码错误，登陆失败
            return Resp.error("您输入的密码不正确！");
        }
        return Resp.success(user1);
    }

    @Override
    public Resp<ArrayList<User>> selectAll() {
        ArrayList<User> list = dao.selectAll();
        if(list == null)
            return Resp.error("查询失败");
        return Resp.success(list);
    }

    @Override
    public Resp<User> Register(User user) {//对用户密码进行加盐加密后保存到数据库
        user.setuName(user.getuName().trim());
        user.setuPw(user.getuPw().trim());
        if(user == null || user.getuName()==null || user.getuPw()==null || user.getuName().length()==0 || user.getuPw().length()==0){
            return Resp.error("请输入合理的信息！");
        }
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random1 = new Random();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < 4; i++) {
            int num = random1.nextInt(str.length());
            char c = str.charAt(num);
            stringBuffer.append(c);
        }
        String random = stringBuffer.toString();
        String pw = user.getuPw();
        pw = pw + random;
        String s = DigestUtils.md5DigestAsHex(pw.getBytes());
        user.setSalt(random);
        user.setuPw(s);
        user.setuSts(0);
        boolean b = dao.add(user);
        if(b)
            return Resp.success(user);
        return Resp.error("注册失败");
    }

    @Override
    public Resp<User> auditInfo(User user) {
        return null;
    }

    @Override
    public Resp<String> updatePwd(User user){
        User user1 = dao.selectByuId(user.getuId());
        String opw = user.getuPw()+user1.getSalt();
//        System.out.println(opw);
        String s = DigestUtils.md5DigestAsHex(opw.getBytes());
//        System.out.println(s);
//        System.out.println(user1.getuPw());
        String pwd = user.getPwd2();
        pwd = pwd + user1.getSalt();
        String s1 = DigestUtils.md5DigestAsHex(pwd.getBytes());
        if (s.equals(user1.getuPw())){
            Boolean b = dao.updatePwd(user.getuId(), s1);
            if (b)  return Resp.success("密码修改成功！");
            else return Resp.error("修改失败！");
        }else
            return Resp.error("您输入的密码错误！");
    }

    @Override
    public Resp<String> updateInfo(UserDetails ud) {
        Boolean b = dao.updateInfo(ud);
        if (b) return Resp.success("信息更新成功！");
        else return Resp.error("修改失败！");
    }

    @Override
    public Resp<String> applyMod(ModApply ma){
        Boolean isself = ma.getIsself();
        if(isself!=null&&isself){
            List<UserDetails> list = dao.selectSelfByuId(ma.getUid());
            if(list.size()>0){
                return Resp.error("本人只能有一个！");
            }
        }
        ma.setApptime(new Date());
        ma.setSt(0);
        Boolean b = dao.applyMod(ma);
        if (b) return Resp.success("信息更新成功！");
        else return Resp.error("修改失败！");
    }



//    @Override
//    public Result<List<ModApply>> selectByCondition(Record r) {
//        return null;
//    }

    @Override
    public Resp<String> audApply(ModApply ma1) {
        ModApply ma = dao.selectModById(ma1.getMid());
        if(ma.getSt()==1)
            return Resp.error("已经审核过了，不能再审核！");
//        System.out.println(ma);
        ma.setSt(ma1.getSt());
        ma.setNote(ma1.getNote());
        ma.setAudtime(new Date());
        UserDetails ud = new UserDetails();
        ud.setName(ma.getName());
        ud.setAddr(ma.getAddr());
        ud.setPhone(ma.getPhone());
        ud.setGender(ma.getGender());
//        ud.setQuarantine(ma.getIsQuarantine());
        ud.setPid(ma.getPid());

        Boolean bb;
        Boolean b = dao.updateAppMod(ma);
        if (b) {
            bb = dao.updateInfo(ud);
            if(!bb){
//                System.out.println(ma.getUid());
                ud.setuId(ma.getUid());
                ud.setSelf(ma.getIsself());
                ud.setQuarantine(false);
                dao.addInfo(ud);
            }
            return Resp.success("审核成功！");
        }
        return Resp.error("审核失败");
    }

    @Override
    public Resp<List<ModApply>> selectAppByCondition(R r) {
        if(!(r.getName() == null||r.getName().equals("")||"".equals(r.getName())))
            r.setName("%"+r.getName()+"%");
        if(!(r.getPid() == null||r.getPid().equals("")||"".equals(r.getPid())))
            r.setPid("%"+r.getPid()+"%");
        List<ModApply> list = dao.selectAppByCondition(r);
        return Resp.success(list);
    }

    @Override
    public Resp<String> addDetails(ModApply ma) {
        Boolean isself = ma.getIsself();
        if(isself){
            List<UserDetails> list = dao.selectSelfByuId(ma.getUid());
            if(list.size()>0){
                return Resp.error("本人只能有一个！");
            }
        }
        ma.setApptime(new Date());
        ma.setSt(0);
        Boolean b = dao.applyMod(ma);
        if (b) return Resp.success("信息提交成功！等待审核");
        else return Resp.error("提交失败！");
    }

    @Override
    public Resp<List<UserDetails>> selectUserByCondition(R r) {
        if(!(r.getName() == null||r.getName().equals("")||"".equals(r.getName())))
            r.setName("%"+r.getName()+"%");
        if(!(r.getPid() == null||r.getPid().equals("")||"".equals(r.getPid())))
            r.setPid("%"+r.getPid()+"%");
        List<UserDetails> users = dao.selectByCondition(r);
        return Resp.success(users);
    }
@Override
    public Resp<String> deleteUser(User user){
        R r = new R();
        r.setuId(user.getuId());
        List<UserDetails> list = dao.selectByCondition(r);
        for (UserDetails userDetails : list) {
            dao.deleteuDetails(userDetails);
        }
        Boolean b = dao.deleteUser(user);
        if (b)  return Resp.success("删除成功");
        return Resp.error("删除失败！");
    }
@Override
    public Resp<String> deleteuDetail(UserDetails ud){
        Boolean b = dao.deleteuDetails(ud);
        if (b)  return Resp.success("删除成功");
        return Resp.error("删除失败！");
    }

    @Override
    public List<UserDetails> selectUdByuId(Integer uId) {
        return dao.selectUdByuId(uId);
//        return details;
    }

    @Override
    public UserDetails selectUdByPid(String pid) {
        return dao.selectUdByPid(pid);
//        return null;
    }
}
